<template>
  <div class="dcsalarmInfo">
  <basic-container>
    <div class="search-tools">
      <label class="search_label">报警时间：</label>
      <!-- 开始日期 -->
      <el-date-picker
        v-model="dataFrom"
        type="datetime"
        size="small"
        placeholder="开始日期"
        :picker-options="pickerOption"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <label class="search_label"> 至 </label>

      <!-- 结束日期 -->
      <el-date-picker
        v-model="dataTo"
        type="datetime"
        size="small"
        placeholder="结束日期"
        :picker-options="pickerOption"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <!-- 查询 -->
      <el-button
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="searchDcspoint" class="buttonLeft">
        查 询
      </el-button>

      <!-- 清空 -->
      <el-button
        size="small"
        icon="el-icon-delete"
        @click="searchReset">
        清 空
      </el-button>
    </div>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot-scope="scope" slot="bjcl">
        <div v-if="scope.row['sfcl']!==1||scope.row['sfcl']==null||scope.row['sfcl']==''||scope.row['sfcl']===undefined">
          <el-button type="danger" size="small" round @click="openCLTC(scope.row)">未处理</el-button>
        </div>
        <div v-else>
          <el-button type="primary" size="small" round >已处理</el-button>

        </div>
      </template>

      <template slot="menuLeft" class="buttonTop">

<!--        删除-->
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.prod_unit_delete"
          @click="handleDelete">
          删 除
        </el-button>
      </template>

    </avue-crud>
    <el-dialog title="报警" :visible.sync="dialogFormVisible" :modal="false">
      <el-form :model="form">
        <el-form-item label="报警核实结果" label-width="100px">
          <el-input v-model="form.hsjg" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="rowSave">确 定</el-button>
      </div>
    </el-dialog>
  </basic-container>
  </div>
</template>

<script>
  import { getDetail, add, update, remove,getPage} from "@/api/mmhi/dcsalarminfo";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        ddsiId:"",
        mcid:"",
        dialogFormVisible:false,
        dataFrom:"",
        dataTo:"",
        startTime:"",
        endTime:"",
        pickerOption: {
          firstDayOfWeek: 1,
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        },
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          // delBtn:true,
          // menuType:true,
          addBtn:false,
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "报警工艺名称",
              prop: "scgymc",
              width:120,
              align:'center',
              editDisplay: false,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=majorSupeHazardChem_scgymc",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              type:'select',
              rules: [{
                required: true,
                message: "请输入报警信息",
                trigger: "blur"
              }]
            },
            {
              label: "所属反应釜",
              prop: "fyfmc",
              editDisplay: false,
              align: "center",
              rules: [{
                required: true,
                message: "请输入主键",
                trigger: "blur"
              }]
            },
            {
              label: "DCS名称",
              prop: "dwmc",
              editDisplay: false,
              align: "center",
              rules: [{
                required: true,
                message: "请输入企业",
                trigger: "blur"
              }]
            },
            {
              label: "报警时间",
              prop: "zdwxbjsj",
              width:140,
              align:'center',
              editDisplay: false,
              rules: [{
                required: true,
                message: "请输入dcs数据点信息",
                trigger: "blur"
              }]
            },
            {
              label: "核实时间",
              prop: "hssj",
              width:140,
              editDisplay: false,
              align: "center",
              rules: [{
                required: true,
                message: "请输入报警值",
                trigger: "blur"
              }]
            },
            {
              label: "核实结果",
              prop: "hsjg",
              align: "center",
              maxlength:127,
              rules: [{
                required: true,
                message: "请输入报警时间",
                trigger: "blur"
              }]
            },
            {
              label: "核实人员",
              prop: "userName",
              align: "center",
              editDisplay: false,
              rules: [{
                required: true,
                message: "请输入核实时间",
                trigger: "blur"
              }]
            },
            {
              label: "报警处理",
              align: "center",
              prop: "bjcl",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              width:130,
              slot: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          // addBtn: this.vaildData(this.permission.dcsalarminfo_add, false),
          viewBtn: this.vaildData(this.permission.dcsalarminfo_view, false),
          delBtn: this.vaildData(this.permission.dcsalarminfo_delete, false),
          editBtn: this.vaildData(this.permission.dcsalarminfo_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    watch:{
      // 开始日期
      dateFrom() {
        if (this.dateFrom) {
          let startDate = new Date(this.dateFrom);
          let endDate = new Date(this.dateTo);
          if (startDate.getTime() > endDate.getTime()) {
            this.dateTo = this.dateFrom;
          }
        }
      },

      // 结束日期
      dateTo() {
        if (this.dateTo) {
          let startDate = new Date(this.dateFrom);
          let endDate = new Date(this.dateTo);
          if (startDate.getTime() > endDate.getTime()) {
            this.dateFrom = this.dateTo;
          }
        }
      }
    },
    methods: {
      //打开处理弹窗
      openCLTC(row){
        this.mcid=row.mcId
        this.ddsiId=row.ddsiId
        this.dialogFormVisible=true
      },
      // 点击查询按钮
      searchDcspoint(loading) {
        if (this.dataFrom>this.dataTo){
          this.$message.warning("结束日期填写错误,需大于开始日期");
          loading();
          return
        }
        this.page.currentPage = 1;
        this.onLoad(this.page);
      },
      //新增
      rowSave(row, done, loading) {
        row.bjz =this.mcid
        row.dcsDataSiteInfoId=this.ddsiId
        row.hsjg=this.form.hsjg
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
        this.dialogFormVisible=false
        this.form={}
      },
      //修改
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      //删除
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      //批量删除
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      //弹窗之前
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      // 清空搜索条件
      searchReset() {
        this.dataFrom=""
        this.dataTo=""
        this.startTime=this.dataFrom
        this.endTime=this.dataTo
        this.query = {};
        this.onLoad(this.page);
      },
      // 搜索条件改变事件
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      // 勾选
      selectionChange(list) {
        this.selectionList = list;
      },
      // 清空勾选
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      // 换页数
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      // 换每页显示数量
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      // 刷新
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        this.query.startTime=this.dataFrom
        this.query.endTime=this.dataTo
        getPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          console.log(this.data)
          this.loading = false;
          this.selectionClear();
        });
        console.log(this.data)
      }
    }
  };
</script>

<style lang="scss">
  .dcsalarmInfo {
    .el-card__body {
      padding: 10px !important;
    }
    .search-tools{
      margin-bottom: 10px;
      .el-input {
        width: 200px;
      }
      .el-button{
        margin-left: 10px;
      }
    }
    .buttonTop{
      margin-top: 10px;
    }
  }
</style>
